/*
 * Copyright (c) [2016] [ <ether.camp> ]
 * This file is part of the ethereumJ library.
 *
 * The ethereumJ library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * The ethereumJ library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
 */

package org.altoro.core.utils;

import lombok.extern.slf4j.Slf4j;
import org.altoro.common.utils.Base58;
import org.altoro.common.utils.Sha256Hash;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class Wallet {


  private static byte addressPreFixByte = (byte) 0x41;   //41 + address

  /**
   * Creates a new Wallet .
   */
  public Wallet() {
  }

  public static byte getAddressPreFixByte() {
    return addressPreFixByte;
  }

  public static String encode58Check(byte[] input) {
    byte[] hash0 = Sha256Hash.hash(input);
    byte[] hash1 = Sha256Hash.hash(hash0);
    byte[] inputCheck = new byte[input.length + 4];
    System.arraycopy(input, 0, inputCheck, 0, input.length);
    System.arraycopy(hash1, 0, inputCheck, input.length, 4);
    return Base58.encode(inputCheck);
  }

  public static byte[] decode58Check(String input) {
    byte[] decodeCheck = Base58.decode(input);
    if (decodeCheck.length <= 4) {
      return null;
    }
    byte[] decodeData = new byte[decodeCheck.length - 4];
    System.arraycopy(decodeCheck, 0, decodeData, 0, decodeData.length);
    byte[] hash0 = Sha256Hash.hash(decodeData);
    byte[] hash1 = Sha256Hash.hash(hash0);
    if (hash1[0] == decodeCheck[decodeData.length] &&
        hash1[1] == decodeCheck[decodeData.length + 1] &&
        hash1[2] == decodeCheck[decodeData.length + 2] &&
        hash1[3] == decodeCheck[decodeData.length + 3]) {
      return decodeData;
    }
    return null;
  }

  public static String formatNameAndSymbol(String format) {
    List<String> strList = Wallet.getStrList(format, 64);
    if (strList.size() != 3) {
      return null;
    }
    return Wallet.hexStringToString(strList.get(2).substring(0, Integer.parseInt(Wallet.delZeroForNum(strList.get(1))) * 2));
  }

  /**
   * 长度不够前面补0
   *
   * @param str
   * @param strLength
   * @return
   */
  public static String addZeroForNum(String str, int strLength) {
    int strLen = str.length();
    if (strLen < strLength) {
      while (strLen < strLength) {
        StringBuffer sb = new StringBuffer();
        sb.append("0").append(str);// 左补0
        // sb.append(str).append("0");//右补0
        str = sb.toString();
        strLen = str.length();
      }
    }
    return str;
  }

  /**
   * 去除所有左边的0
   * @param str
   * @return
   */
  public static String delZeroForNum(String str) {
    return str.replaceAll("^(0+)", "");
  }

  /**
   * 16进制字符串转换为字符串
   *
   * @param s
   * @return
   */
  public static String hexStringToString(String s) {
    if (s == null || s.equals("")) {
      return null;
    }
    s = s.replace(" ", "");
    byte[] baKeyword = new byte[s.length() / 2];
    for (int i = 0; i < baKeyword.length; i++) {
      try {
        baKeyword[i] = (byte) (0xff & Integer.parseInt(
                s.substring(i * 2, i * 2 + 2), 16));
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    try {
      s = new String(baKeyword, "gbk");
      new String();
    } catch (Exception e1) {
      e1.printStackTrace();
    }
    return s;
  }

  /**
   * 把原始字符串分割成指定长度的字符串列表
   *
   * @param inputString
   *            原始字符串
   * @param length
   *            指定长度
   * @return
   */
  public static List<String> getStrList(String inputString, int length) {
    int size = inputString.length() / length;
    if (inputString.length() % length != 0) {
      size += 1;
    }
    return getStrList(inputString, length, size);
  }

  /**
   * 把原始字符串分割成指定长度的字符串列表
   *
   * @param inputString
   *            原始字符串
   * @param length
   *            指定长度
   * @param size
   *            指定列表大小
   * @return
   */
  public static List<String> getStrList(String inputString, int length,
                                        int size) {
    List<String> list = new ArrayList<String>();
    for (int index = 0; index < size; index++) {
      String childStr = substring(inputString, index * length,
              (index + 1) * length);
      list.add(childStr);
    }
    return list;
  }

  /**
   * 分割字符串，如果开始位置大于字符串长度，返回空
   *
   * @param str
   *            原始字符串
   * @param f
   *            开始位置
   * @param t
   *            结束位置
   * @return
   */
  public static String substring(String str, int f, int t) {
    if (f > str.length())
      return null;
    if (t > str.length()) {
      return str.substring(f, str.length());
    } else {
      return str.substring(f, t);
    }
  }



}
